package com.drools.drools.query;

import com.drools.drools.BaseTest;
import com.drools.drools.model.Person;
import org.junit.Test;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.rule.QueryResults;
import org.kie.api.runtime.rule.QueryResultsRow;

/**
 * Created with IntelliJ IDEA.
 * User: mishuai
 * Date: 2019-05-09
 * Time: 08:31
 * Des:
 */
public class QueryTest extends BaseTest {

    @Test
    public void test(){
        KieSession kieSession=this.getKieSessionBySessionName("query-rules");
        Person person1=new Person(21);
        person1.setName("zhang");

        Person person2=new Person(23);
        person2.setName("li");
        kieSession.insert(person1);
        kieSession.insert(person2);
        kieSession.fireAllRules();
        QueryResults queryResults=kieSession.getQueryResults("query-by-age");
        System.out.println(queryResults.size());
        for(QueryResultsRow resultsRows:queryResults){
            Person p= (Person) resultsRows.get("$p1");
            System.out.println("personname"+p.getName());
        }

        QueryResults queryResults1=kieSession.getQueryResults("query-by-param",20);
        System.out.println(queryResults1.size());
        for(QueryResultsRow resultsRows:queryResults1){
            Person p= (Person) resultsRows.get("$p2");
            System.out.println("personname"+p.getName());
        }
        kieSession.dispose();
    }
}
